<?php
//$_['action']={query|set|delete|list}
//	{uid}

//query:
//$_['node_id']
//$_['key']
//return raw_value

//set:
//$_['node_id']
//$_['key']
//$_['type']
//$_['value']
//return 'success'|''

//delete:
//$_['node_id']
//$_['key']
//return 'success'|''

//list:
//$_['node_id']
//return encode(key)<br>encode(value)<hr>...
//$_['node_id']
//$_['type']
//return key<br>value<hr>...
//$_['type']
//return node_id<br>key<br>value<hr>...

//uid:
//return user_id|''
	include 'include/function.php';
	
	$input=array(
		'action'=>get_request('action'),
		'node_id'=>get_request('node_id'),
		'type'=>get_request('type'),
		'key'=>get_request('key'),
		'value'=>get_request('value'),
		'ok'=>get_request('ok')
	);
	
	if($input['action']=='query' || $input['action']=='set') {
		if(empty($input['node_id'])||empty($input['key'])||(!is_numeric($input['node_id'])))die();
		$id=$input['node_id'];
		$key=mysql_real_escape_string($input['key']);
		$query="select value from node_custom where node_id=$id and `key`='$key' limit 1 ";
		$row=mysql_fetch_row(oj_query($query));
		if($input['action']=='query')
			echo $row[0]; //raw data
		else { //set
			if(empty($input['ok']))die(); //没发送完整
			if(!empty($input['value']))$value=mysql_real_escape_string($input['value']);
			if(!empty($input['type'])) $type=mysql_real_escape_string($input['type']);
			if(isset($row[0])) {
				if(!(isset($value)||isset($type)))die();
				$query='update node_custom set ' . (isset($value)?"value='$value'":'') 
					. (isset($value)&&isset($type)?', ':'') . (isset($type)?"type='$type'":'')
					. " where node_id=$id and `key`='$key' ";
			} else {
				if(!isset($type))$type='undefined';
				$query="insert into node_custom (node_id,`key`,value,type) values ($id,'$key','$value','$type') ";
			}
			oj_query($query);
			echo 'success';
		}
	} else if($input['action']=='delete') {
		if(empty($input['node_id'])||empty($input['key'])||(!is_numeric($input['node_id'])))die();
		$id=$input['node_id'];
		$key=mysql_real_escape_string($input['key']);
		$query="select id from node_custom where node_id=$id and `key`='$key' limit 1 ";
		$row=mysql_fetch_row(oj_query($query));
		if(!isset($row[0]))die();
		$query="delete from node_custom where id={$row[0]} limit 1 ";
		oj_query($query);
		echo 'success';
	} else if($input['action']=='list') {
	} else if($input['action']=='uid') {
		echo $_user_id;
	}

?>
